package com.healthcloud.controller;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.constants.PagineConsts;
import com.healthcloud.entity.SysMenuEntity;
import com.healthcloud.service.SysMenuService;
import com.healthcloud.utils.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统菜单
 * <p>
 * Created by yu on 2017/6/6.
 */
@ApiIgnore
@Controller
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 所有菜单列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public String list(Model model) {

        PageParam pageParam = new PageParam();
        pageParam.setiDisplayStart(PagineConsts.iDisplayStart);
        pageParam.setiDisplayLength(PagineConsts.iDisplayLength);

        //查询列表数据
        List<SysMenuEntity> menuList = null;

        //只有超级管理员，才能查看所有管理员列表
        if (getUser().getUsername().equals(Constant.ADMIN)) {
            menuList = sysMenuService.queryList(new SysMenuEntity(), pageParam);
        } else {
            menuList = sysMenuService.queryUserList(getUserId());
        }

        model.addAttribute("menuList", menuList);

        return "sys/menu/menuMgr";
    }

    /**
     * 所有菜单列表
     */
    @RequestMapping("/search")
    @ResponseBody
    public Map<String, Object> seartList(@ModelAttribute("pageParam") PageParam pageParam, SysMenuEntity entity) {

        //查询列表数据
        List<SysMenuEntity> configList = sysMenuService.queryList(entity, pageParam);
//        logger.info("getType==>"+entity.getType());
//        logger.info("getMenuId==>"+entity.getMenuId());
//        logger.info("getParentId==>"+entity.getParentId());
//        logger.info("getName==>"+entity.getName());

        int total = sysMenuService.queryTotal(entity);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dataList", configList);
        map.put("iTotalRecords", total);
        map.put("iTotalDisplayRecords", total);
        map.put("sEcho", pageParam.getsEcho());

        return map;
    }



    /**
     * 保存
     */
    @RequestMapping("/tosave")
    @RequiresPermissions("sys:menu:save")
    public String save(Model model) {


        PageParam pageParam = new PageParam();
        pageParam.setiDisplayStart(PagineConsts.iDisplayStart);
        pageParam.setiDisplayLength(PagineConsts.iDisplayLength);

        //查询列表数据
        List<SysMenuEntity> menuList =sysMenuService.queryNotButtonList();
        for(SysMenuEntity menuEntity : menuList){
            menuEntity.setOpen(true);
        }

        model.addAttribute("menuList", menuList);
        return "sys/menu/addOrUpdateMenu";
    }

    @SysLog("保存菜单")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes save(SysMenuEntity menuEntity) {

        //数据校验
        verifyForm(menuEntity);

        sysMenuService.save(menuEntity);

        return ApiRes.SUCCESS;

    }

    /**
     * 修改
     */
    @RequestMapping("/toupdate")
    @RequiresPermissions("sys:menu:update")
    public String update(@Param("menuId") String menuId,Model model){

        PageParam pageParam = new PageParam();
        pageParam.setiDisplayStart(PagineConsts.iDisplayStart);
        pageParam.setiDisplayLength(PagineConsts.iDisplayLength);

        SysMenuEntity _menuEntity= sysMenuService.queryObjectByid(Long.valueOf(menuId));

        //查询列表数据
        List<SysMenuEntity> menuList =sysMenuService.queryNotButtonList();
        for(SysMenuEntity menuEntity : menuList){
            menuEntity.setOpen(true);

            if(menuEntity.getMenuId().equals(_menuEntity.getParentId())){
                menuEntity.setChecked(true);
            }
        }


        model.addAttribute("menuList", menuList);
        model.addAttribute("entity", _menuEntity);

        return "sys/menu/addOrUpdateMenu";
    }

    /**
     * 修改
     */
    @SysLog("修改菜单")
    @RequestMapping("/update")
    @RequiresPermissions("sys:menu:update")
    @ResponseBody
    public ApiRes update(SysMenuEntity menu) {
        //数据校验
        verifyForm(menu);

        sysMenuService.update(menu);

        return ApiRes.SUCCESS;
    }

    /**
     * 删除
     */
    @SysLog("删除菜单")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:menu:delete")
    @ResponseBody
    public ApiRes delete(Long[] menuIds) {

        sysMenuService.deleteBatch(menuIds);

        return ApiRes.SUCCESS;
    }


    /**
     * 验证参数是否正确
     */
    private void verifyForm(SysMenuEntity menu) {
//        if(StringUtils.isBlank(menu.getName())){
//            throw new RRException("菜单名称不能为空");
//        }
//
//        if(menu.getParentId() == null){
//            throw new RRException("上级菜单不能为空");
//        }

//        //菜单
//        if(menu.getType() == Constant.MenuType.MENU.getValue()){
//            if(StringUtils.isBlank(menu.getUrl())){
//                throw new RRException("菜单URL不能为空");
//            }
//        }

//        //上级菜单类型
//        int parentType = Constant.MenuType.CATALOG.getValue();
//        if(menu.getParentId() != 0){
//            SysMenuEntity parentMenu = sysMenuService.queryObjectByid(menu.getParentId());
//            parentType = parentMenu.getType();
//        }
//
//        //目录、菜单
//        if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
//                menu.getType() == Constant.MenuType.MENU.getValue()){
//            if(parentType != Constant.MenuType.CATALOG.getValue()){
//                throw new RRException("上级菜单只能为目录类型");
//            }
//            return ;
//        }
//
//        //按钮
//        if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
//            if(parentType != Constant.MenuType.MENU.getValue()){
//                throw new RRException("上级菜单只能为菜单类型");
//            }
//            return ;
//        }
    }
}